define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/member/index' + location.search,
                    add_url: 'member/member/add',
                    edit_url: 'member/member/edit',
                    del_url: 'member/member/del',
                    multi_url: 'member/member/multi',
                    import_url: 'member/member/import',
                    multi_pop_url: 'member/member/multipop',
                    table: 'member',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'company_name', title: __('Company_name'), operate: 'LIKE'},
                        {field: 'legal_person_name', title: __('Legal_person_name'), operate: 'LIKE'},
                        {field: 'user.real_name', title: "所属用户", formatter:function (value, row, index) {
                            // console.log(row);
                            if(value){
                                if (index % 2 === 0) {
                                    return '<span style="color:green">' + value + '</span>';
                                } else {
                                    return '<span style="color:olive">' + value + '</span>';
                                }
                            }
                        }},
                        // {field: 'shareholder_name', title: __('Shareholder_name'), operate: 'LIKE'},
                        {field: 'phone1', title: __('Phone'), operate: 'LIKE'},
                        // {field: 'phone2', title: __('Phone2'), operate: 'LIKE'},
                        // {field: 'phone3', title: __('Phone3'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'is_high_tech', title: __('Is_high_tech'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'establishment_date', title: __('Establishment_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'site_scale', title: __('Site_scale'), operate: 'LIKE'},
                        // {field: 'business_sector', title: __('Business_sector'), operate: 'LIKE'},
                        // {field: 'region', title: __('Region'), operate: 'LIKE'},
                        // {field: 'address', title: __('Address'), operate: 'LIKE'},
                        // {field: 'tax_info', title: __('Tax_info'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        // {field: 'personal_credit_doc_file', title: __('Personal_credit_doc_file'), operate: false, formatter: Table.api.formatter.file},
                        // {field: 'enterprise_credit_doc_file', title: __('Enterprise_credit_doc_file'), operate: false, formatter: Table.api.formatter.file},
                        // {field: 'house_cert_images', title: __('House_cert_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'driving_license_images', title: __('Driving_license_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'equipment_invoice_images', title: __('Equipment_invoice_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'business_license_images', title: __('Business_license_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'id_card_images', title: __('Id_card_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'personal_bank_statements_images', title: __('Personal_bank_statements_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'business_bank_statements_images', title: __('Business_bank_statements_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'site_video_images', title: __('Site_video_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'site_photo_images', title: __('Site_photo_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'other_images', title: __('Other_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            /**
             * 批量弹窗
             */
            $(document).on("click", ".btn-multi-pop", function (e) {
                // 获取选中的列表ID
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.multi_pop_url + "?ids=" + ids, '批量生成弹窗')
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'member/member/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'member/member/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'member/member/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        multipop: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
